package hn.cch.spring;

import org.apache.commons.dbcp2.BasicDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource(value = {"classpath:jdbc.properties"}, ignoreResourceNotFound = true)
public class SpringJdbcProperties {

    private Logger logger = LoggerFactory.getLogger(SpringJdbcProperties.class);

    @Value("${master.jdbc.driver}")
    private String master_jdbc_driver;
    @Value("${master.jdbc.url}")
    private String master_jdbc_url;
    @Value("${master.jdbc.username}")
    private String master_jdbc_username;
    @Value("${master.jdbc.password}")
    private String master_jdbc_password;


    @Value("${replica.jdbc.driver}")
    private String replica_jdbc_driver;
    @Value("${replica.jdbc.url}")
    private String replica_jdbc_url;
    @Value("${replica.jdbc.username}")
    private String replica_jdbc_username;
    @Value("${replica.jdbc.password}")
    private String replica_jdbc_password;

    @Bean
    public BasicDataSource master(){
        logger.info("master");
        BasicDataSource master = new BasicDataSource();

        master.setDriverClassName(master_jdbc_driver);
        master.setUrl(master_jdbc_url);
        master.setUsername(master_jdbc_username);
        master.setPassword(master_jdbc_password);

        return master;
    }


    @Bean
    public BasicDataSource replica(){
        logger.info("replica");
        BasicDataSource replica = new BasicDataSource();

        replica.setDriverClassName(replica_jdbc_driver);
        replica.setUrl(replica_jdbc_url);
        replica.setUsername(replica_jdbc_username);
        replica.setPassword(replica_jdbc_password);

        return replica;
    }


    public String getMaster_jdbc_driver() {
        return master_jdbc_driver;
    }

    public void setMaster_jdbc_driver(String master_jdbc_driver) {
        this.master_jdbc_driver = master_jdbc_driver;
    }
}
